/*
 * 2006-1-3 lxm 创建。 
 */
package com.hd123.rumba.organization;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.ParseException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import net.sf.hibernate.HibernateException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.DBaseEntity;
import com.hd123.rumba.runtime.IPersistent;
import com.hd123.rumba.runtime.IXMLPersistent;
import com.hd123.rumba.runtime.NamingMgr;

/**
 * 组织角色 | 实体 | 数据交换类
 * <p>
 * 
 * 对于未来所有组织角色的子类的所有附加属性，通过序列化（xml）的方式传递。涉及到两个属性： <code>_intfMgrName</code>用于标识子类的类型，具体写入的为子类对应的管理员接口类；
 * <code>_data</code>存放被序列化的数据。
 * <p>
 * 
 * 暂时不传递profile属性。
 * 
 * @author lxm
 * 
 */
public class DOrgRole extends DBaseEntity {

  private String type;
  private String organizationUuid;

  private String _intfMgrName;
  private String _data;

  public void assignFrom(IPersistent source) {
    super.assignFrom(source);
    if (source instanceof IOrgRole) {
      this.type = exportInteger(source, "getType");

      try {

        IOrganizationMgr orgMgr = (IOrganizationMgr) NamingMgr.getInstance().lookupMgr(
            IOrganizationMgr.class);
        IOrgRoleMgr roleMgr = orgMgr.lookupOrgRoleMgr((IOrgRole) source);
        if (roleMgr != null) {
          Class[] intfs = roleMgr.getClass().getInterfaces();
          if (intfs.length > 0)
            this._intfMgrName = intfs[0].getName();
        }

        IXMLPersistent srcx = (IXMLPersistent) source;
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.newDocument();
        Element e = doc.createElement(srcx.getXMLTagName());
        srcx.writeToXML(doc, e);
        doc.appendChild(e);
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "gb2312"); 
        t.setOutputProperty(OutputKeys.INDENT, "yes");  
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        t.transform(new DOMSource(doc), new StreamResult(bs));
        
//        File f = new File("c:\\temp\\cc.xml");
//        t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(f)));
        this._data = bs.toString();

//        File f2 = new File("c:\\temp\\dd.xml");
//        FileOutputStream fs = new FileOutputStream(f2);
//        fs.write(this._data.getBytes());
//        fs.flush();
//        fs.close();
      } catch (Exception e) {
      }
    }
  }

  public void assignTo(IPersistent target) throws ParseException, BusinessException,
      HibernateException {
    super.assignFrom(target);
    if (target instanceof IOrgRole) {
      importInteger(target, "setType", this.type);

      try {
        IXMLPersistent tgtx = (IXMLPersistent) target;
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        ByteArrayInputStream bs = new ByteArrayInputStream(this._data.getBytes());
        Document doc = builder.parse(bs);
        for (Node node = doc.getFirstChild(); node != null; node = node.getNextSibling()) {
          if (node instanceof Element) {
            Element e = (Element) node;
            if (e.getTagName().equals(tgtx.getXMLTagName())) {
              tgtx.readFromXML(e);
              break;
            }
          }
        }
      } catch (Exception e) {
        throw new BusinessException(e.getMessage());
      }
    }
  }

  public String get_data() {
    return _data;
  }

  public void set_data(String _data) {
    this._data = _data;
  }

  public String get_intfMgrName() {
    return _intfMgrName;
  }

  public void set_intfMgrName(String mgrName) {
    _intfMgrName = mgrName;
  }

  public String getOrganizationUuid() {
    return organizationUuid;
  }

  public void setOrganizationUuid(String organizationUuid) {
    this.organizationUuid = organizationUuid;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

}
